local zhangfengyu = fk.CreateSkill {
  name = "lingling__zhangfengyu",
}

Fk:loadTranslationTable {
  ["lingling__zhangfengyu"] = "掌风雨",
  [":lingling__zhangfengyu"] = "出牌阶段限一次，你可以选择一名其他角色，若你描述字数比其：多，你弃置其一张牌；多50字，你获得其一张牌；多100字，你对其造成1点伤害。" ..
  "若其多于你，则每多于你一倍，你摸一张牌。",

  ["#lingling__zhangfengyu"] = "掌风雨：选择一名角色，根据其描述字数执行选项！",
  ["#lingling__zhangfengyu1"] = "掌风雨：请选择一张牌弃置！",
  ["#lingling__zhangfengyu2"] = "掌风雨：请选择一张牌获得！",

  ["lingling__zhangfengyu-tip"] = "执行%arg项",
  ["lingling__zhangfengyu-tip-draw"] = "摸%arg张",
}

local U = require "packages/utility/utility"

---@param target Player
local getSkillWords = function(target) --获得一名角色的描述字数
  local skill_list = target:getSkillNameList()
  local nums = 0
  if #skill_list > 0 then
    for _, skill in ipairs(skill_list) do
      local str = Fk:getDescription(skill, "zh_CN", target):gsub("<[^>]+>", "")
      nums = nums + str:len()
    end
  end
  return nums
end

zhangfengyu:addEffect("active", {
  anim_type = "control",
  card_num = 0,
  target_num = 1,
  prompt = "#lingling__zhangfengyu",
  can_use = function(self, player)
    return player:usedSkillTimes(zhangfengyu.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected)
    return #selected == 0 and to_select ~= player
  end,
  target_tip = function(self, player, to_select, selected, _, _, selectable, _)
    if not selectable then return end
    local x = getSkillWords(player) - getSkillWords(to_select)
    local y = 0
    if x > 0 then
      y = math.min(math.floor((x + 50)/50), 3)
    end
    local n = math.floor((getSkillWords(to_select) - 1) / getSkillWords(player))
    if #selected == 0 and x > 0 then
      return Fk:translate("lingling__zhangfengyu-tip:::" .. U.ConvertNumber(y))
    end
    if #selected == 0 and n > 0 then
      return Fk:translate("lingling__zhangfengyu-tip-draw:::" .. U.ConvertNumber(n))
    end
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local x = getSkillWords(player) - getSkillWords(target)
    print(x)
    local n = math.floor((getSkillWords(target) - 1) / getSkillWords(player))
    if n > 0 then
      player:drawCards(n, zhangfengyu.name)
      return
    end
    if x > 0 and not target:isNude() and not target.dead and not player.dead then
      local card = room:askToChooseCard(player, {
        target = target,
        flag = "he",
        skill_name = zhangfengyu.name,
        prompt = "#lingling__zhangfengyu1"
      })
      room:throwCard(card, zhangfengyu.name, target, player)
    end
    if x > 49 and not target:isNude() and not target.dead and not player.dead then
      local card = room:askToChooseCard(player, {
        target = target,
        flag = "he",
        skill_name = zhangfengyu.name,
        prompt = "#lingling__zhangfengyu2"
      })
      room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonPrey, zhangfengyu.name, nil, false, player)
    end
    if x > 99 and not target.dead and not player.dead then
      room:damage {
        damage = 1,
        from = player,
        to = target,
        skillName = zhangfengyu.name
      }
    end
  end,
})

return zhangfengyu
